/**
 * 渠道商
 * */


(function () {
})();


//商品选项
function getOption(){
    $.ajax({
        type: apis.qdCardGoption.method,
        url: apis.qdCardGoption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data.card;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '</option>';
            }
            $('#cardid').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//商品列表
function getList(){
    $.ajax({
        type: apis.qdCardGlist.method,
        url: apis.qdCardGlist.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            indexHtml(d);
            $('#pageTotal').html(d.length);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//商品字符串
function indexHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].card_name + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>' + d[i].code + '</td>';
        h += '<td>';
        if (d[i].imgLink == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].imgPrefix + d[i].imgLink + '" height="50"/>';
        }
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="card_edit(\'编辑\',\'card-goods-edit.html\', ' + d[i].id + ');" style="text-decoration: none">';
        h += '编辑 <i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="card_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除 <i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="price' + d[i].id + '" value="' + d[i].price + '"/>';
        h += '<input type="hidden" id="num' + d[i].id + '" value="' + d[i].num + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//编辑
function card_add(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,500);
}


//编辑
function card_edit(title,url,id,w,h){
    url += '?id=' + id;
    var title = $('#title' + id).val();
    var price = $('#price' + id).val();
    var num = $('#num' + id).val();
    url += '&title=' + escape(title);
    url += '&price=' + price;
    url += '&num=' + num;
    layer_show(title,url,w,500);
}


//更新
function modify(){
    var formData = {
        id: id,
        cardid: $('#cardid').val(),
        title: $('#title').val(),
        price: $('#price').val(),
        num: $('#num').val(),
    };
    console.log(formData);
    if (!formData.cardid) {
        //layer.msg('SKU规格必须的',{icon: 0,time: 1000});
        msgPop({msg: '编号必须的'});
        return
    }
    if (!formData.title) {
        //layer.msg('SKU规格必须的',{icon: 0,time: 1000});
        msgPop({msg: '名称必须的'});
        return
    }
    //return
    $.ajax({
        type: apis.qdCardGmodify.method,
        url: apis.qdCardGmodify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function card_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.qdCardGdel.method,
            url: apis.qdCardGdel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    //msgPop({msg: res.msg});
                    layerMsgErr(res.msg);
                    return
                }
                //layer.msg('商品已删除!',{icon: 1,time: 1000});
                layerMsgOk('删除成功');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}